package di9;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.StringTokenizer;

import javax.swing.*;
public class HandleListener implements ActionListener {
   JTextArea inputText,showText;


   public void setInputText(JTextArea text) {
      inputText = text;
   }
   public void setShowText(JTextArea text) {
      showText = text;
   } 
   public void actionPerformed(ActionEvent e) {
      String str=e.getActionCommand(); 
      if(str.equals("copy"))
         showText.copy();
      else if(str.equals("cut"))
         showText.cut();
      else if(str.equals("paste"))
         inputText.paste();
      else if(str.equals("replace")){
    	  replace();
      }
    
      
      else if(str.equals("open")){
    	  File file=new File("e:\\a.txt");
  	 try {
  		 Reader in = new FileReader(file);
  		 BufferedReader bufferedReader =new BufferedReader(in);
  		 String str3=null;
  		 while((str3=bufferedReader.readLine())!=null) {
  			 
  			 showText.append(str3+",");
  			 
  		 }
  	 }catch(IOException e1) {
  		 e1.printStackTrace();
  		 JOptionPane.showMessageDialog(null, "打开失败");
  	 }
       JOptionPane.showMessageDialog(null, "打开成功");
  	 
   }

      else if(str.equals("save")) {
    	  File file=new File("e:\\a.txt");
      try{  
    	  Writer out = new FileWriter(file);
      BufferedWriter bufferedWriter = new BufferedWriter(out);
      String str1=showText.getText();
      String str2[]=str1.split(",");
      for(int i=0;i<str2.length;i++){
           bufferedWriter.write(str2[i]);
         bufferedWriter.newLine();
      } 
      
      bufferedWriter.close(); 
              out.close();
      }catch(IOException e1) {
    	  e1.printStackTrace();
    	  JOptionPane.showMessageDialog(null, "保存失败");
      }
      JOptionPane.showMessageDialog(null, "保存成功");
   }
   }
private void replace() {
	// TODO Auto-generated method stub
	String oldString = JOptionPane.showInputDialog("输入");
	String newString = JOptionPane.showInputDialog("新");
	showText.setText(inputText.getText().replace(oldString, newString));
	File fWrite = new File("e:\\a.txt");
	
	try{ Writer out = new FileWriter(fWrite);
	BufferedWriter bufferedWriter = new BufferedWriter(out);
	bufferedWriter.write(inputText.getText());
	bufferedWriter.newLine();
	bufferedWriter.close();
	out.close();
	JOptionPane.showMessageDialog(showText,"成功","提示",JOptionPane.WARNING_MESSAGE);
	}catch(IOException e1){
	}
}

}


